<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css" />
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<link rel="stylesheet" type="text/css" href="../../css/btnplus.css" />
		<style type="text/css">
			html,body{
				background-color: #F7F8FA;
				
			}
			.order_route{ padding: 5px 15px 40px;}
			.order_route b{ display:block; font-size: 12px; color: #808080; font-weight: 400; line-height: 35px; height: 35px; background: url(../../image/img_19.png) no-repeat left center; background-size: 15px auto; padding-left: 20px;}
			.order_route li{ margin-bottom: 10px; background-color: #fff; border-radius: 4px; padding: 10px; position: relative; overflow: hidden;}
			.order_route h4{ display: block; font-size: 16px; font-weight: 600; color: #333; line-height: 35px; height: 35px;}
			.order_route h4 img,.order_reminder h4 img,.order_list .con img{ display:inline-block; vertical-align: top; height: 5px; margin: 15px 5px 0;}
			.order_route a{ position:absolute; right: 10px; top: 11px; width: 33px; height: 33px; background-image: url(../../image/img_20.png); background-size: 100% 100%;}

			.f_btn{ position: fixed; left: 0; right: 0; bottom: 0; z-index: 9;}
			.f_btn button{ height: 50px;}
			.sk-three-bounce {
			  margin: 0 auto;
			  width: 80px;
			  text-align: center; }
			  .sk-three-bounce .sk-child {
				width: 13.5px;
				height: 13.5px;
				background-color: #3262F1;
				border-radius: 100%;
				display: inline-block;
				-webkit-animation: sk-three-bounce 1.4s ease-in-out 0s infinite both;
						animation: sk-three-bounce 1.4s ease-in-out 0s infinite both; }
			  .sk-three-bounce .sk-bounce1 {
				-webkit-animation-delay: -0.32s;
						animation-delay: -0.32s; }
			  .sk-three-bounce .sk-bounce2 {
				-webkit-animation-delay: -0.16s;
						animation-delay: -0.16s; }

			@-webkit-keyframes sk-three-bounce {
			  0%, 80%, 100% {
				-webkit-transform: scale(0);
						transform: scale(0); }
			  40% {
				-webkit-transform: scale(1);
						transform: scale(1); } }

			@keyframes sk-three-bounce {
			  0%, 80%, 100% {
				-webkit-transform: scale(0);
						transform: scale(0); }
			  40% {
				-webkit-transform: scale(1);
						transform: scale(1); }
			}
			.more{ padding: 1.20rem 0; text-align: center; font-size: 0.26rem; color: #999; line-height: 0.60rem;}
			.display-none{ display:none; }
			.text-center { text-align: center !important; }
		</style>
	</head>
	<body>
		<div id="result">
		<div class="order_route">
			<b>当前接单路线</b>
			<ul id="lineslist">
			</ul>
			<script id="lineslist_template" type="text/template">
				{{ for (var i = 0, len = it.length; i < len; i++) { }}
				<li><h4>{{=it[i].sendarea}} <img src="../../image/img_21.png" alt="" /> {{=it[i].destarea}}</h4><a href="javascript:;" tapmode onclick="dellines(this,{{=it[i].id}});" class="del"></a></li>
				{{ } }}
			</script>
		</div>
		<!--当前接单路线end-->

		<!--按钮start-->
		<div class="f_btn">
			<button class="d_btn" tapmode="btnpress" id="btnadd" onclick="addlines();">添加接单路线</button>
		</div>
		</div>
		<div class="text-center display-none" style="padding: 110px 0 !important;" id="divnodata">
			<div class="text-center"><img src="../../image/nolines.png" style="margin:0 auto;width:41px;height:auto;"></div>
			<p class="text-center" style="font-size:12px;color:#9f9f9f;margin-top:15px;">暂时没有线路</p>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/app.js"></script>
	<script type="text/javascript" src="../../script/doT.js"></script>
	<script type="text/javascript">
		var page = 1;
		var canadd = 0;
		var params = {
			where: {
				status: 1,
			},
			page: 1
		};
		var UILoading;
		apiready = function() {
			api.parseTapmode();
			UILoading = api.require('UILoading');
			$api.css($api.dom('#result'), 'display: none');
			showloading();
			getJsonFromAPI();
			
			api.addEventListener({
                name: 'navitembtn'
            }, function(ret, err) {
                if (ret.type == 'left') {
					api.closeWin({
					});
                }
            });
			//api.addEventListener({
				//name:"scrolltobottom"
			//},function(ret,err){
				//getJsonFromAPI();
			//});
			
			api.addEventListener({
				name:"refreshlines"
			},function(ret,err){
				refreshPage();
			});
			api.addEventListener({
				name : 'reloadpage'
			}, function(ret, err) {
				refreshPage();				
			});
		}
		function addlines() {
			if(canadd == 1)
			{
				openNavWin('#fff','addlines_win','addlines_frm.html','接单线路',false,false,true,[],{areatype:'send',fromwin:'mylines_win'});
			}
		}
		function dellines(obj,id) {
			api.confirm({
				title: '删除线路',
				msg: '是否删除该线路？',
				buttons: ['取消', '确定']
			}, function(ret, err) {
				var index = ret.buttonIndex;
				if(index == 2)
				{
					AjaxData('del',id);
					$api.remove(obj.parentNode);
				}
			});
		}
		function AjaxData(type,linesid) {
			//删除
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret.value) {
					var userinfo = JSON.parse(ret.value);
					api.ajax({
						url: ApiUrl + '/v1/lines',
						method: 'post',
						cache: false,
						headers: {
							authorization: kAppKey+';'+userinfo.access_token+';'+userinfo.uid
						},
						data: {
							values: {
								linesid: linesid,
								type: type
							}
						}
					}, function(ret, err) {
						if (ret) {
							
							toast(ret.msg);
							if(type == 'del')
							{
								api.sendEvent({
									name : 'refreshlines'
								});
							}
						} else {
							if(err.statusCode==402)
							{
								Relogin();
							}
							else
							{
								console.log(JSON.stringify(err));
								//toast('网络异常');
							}
						}
					});
				}
			});
		}
		function refreshPage() {
			page = 1;
			getJsonFromAPI();
		}
		function getJsonFromAPI() {
			//从接口获取查询数据
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret.value) {
					var userinfo = JSON.parse(ret.value);
					api.ajax({
						url: ApiUrl + '/v1/lines/'+page+'/'+ $api.jsonToStr(params),
						method: 'get',
						cache: false,
						timeout: 10,
						headers: {
							authorization: kAppKey+';'+userinfo.access_token+';'+userinfo.uid
						}
					}, function(ret, err) {
						hideloading();
						api.refreshHeaderLoadDone();
						$api.css($api.dom('#result'), 'display: block');
						if (ret) {
							if(ret.list.length < 10)
							{
								canadd = 1;
								$api.css($api.byId('btnadd'),'opacity:1');
							}
							else
							{
								canadd = 0;
								$api.css($api.byId('btnadd'),'opacity:0.52');
							}
							if(ret.list.length > 0)
							{
								$api.addCls($api.byId('divnodata'),'display-none');
								var isAppend = true;
								if(page==1)
								{
									isAppend = false;
								}
								page++;
								var lineslist = ret.list;
								UpdateList('lineslist','lineslist_template',lineslist,isAppend);
							}
							else
							{
								if(page==1)
								{
									UpdateList('lineslist','lineslist_template',ret.list,false);
									$api.removeCls($api.byId('divnodata'),'display-none');
								}
							}
							
						} else {
							if(err.statusCode==402)
							{
								setTimeout(function(){
									openNavWin('#fff','login_win','../login/login_frm.html','',true,false,false,[],{});
								}, 0);
							}
							else
							{
								console.log(JSON.stringify(err));
								toast('网络异常');
							}
						}
					});
				}
			});
		}
		//渲染数据
		function UpdateList(domId,templateId,jsondata,isAppend) {
			//console.log(JSON.stringify(jsondata));
			var act_content = $api.byId(domId);
			var tempFn = doT.template($api.byId(templateId).innerHTML);
			var resultText = tempFn(jsondata);
			if(isAppend)
			{
				$api.append(act_content, resultText);
			}
			else
			{
				$api.html(act_content, resultText);
			}
			api.parseTapmode();
		}
	</script>
</html>